package com.jiaai.cloud.base.weixin.util;


import com.jiaai.cloud.base.weixin.callback.EvenCallback;
import com.jiaai.cloud.base.weixin.callback.MessageCallback;
import com.jiaai.cloud.base.weixin.event.*;
import com.jiaai.cloud.base.weixin.message.resp.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

public class CoreService {
    /**
     * 处理微信发送过来的消息
     * @param request
     * @param messageCallback
     * @param evenCallback
     * @throws Exception 
     */
    public static String processRequest(HttpServletRequest request, MessageCallback messageCallback, EvenCallback evenCallback) throws Exception {
		    // xml格式的消息数据
		    String respXml = null;
   
		    // 调用parseXml方法解析请求消息
            Map<String, String> requestMap = MessageUtil.parseXml(request);
            // 发送方帐号
            String fromUserName = requestMap.get("FromUserName");
            // 开发者微信号
            String toUserName = requestMap.get("ToUserName");
            // 消息类型
            String msgType = requestMap.get("MsgType");
            
            if(msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_EVENT) && evenCallback!=null){
            	String eventType = requestMap.get("Event");
            	BaseEvent baseEvent=new BaseEvent();
            	baseEvent.setFromUserName(fromUserName);
            	baseEvent.setCreateTime(Long.parseLong(requestMap.get("CreateTime")));
            	baseEvent.setMsgType(msgType);
            	baseEvent.setToUserName(toUserName);
            	baseEvent.setEvent(eventType);
            	
            	switch (eventType) {
            	case MessageUtil.EVENT_TYPE_SUBSCRIBE://关注
            			SubscribeEvent subscribeEvent=new SubscribeEvent();
            			subscribeEvent.setFromUserName(baseEvent.getFromUserName());
            			subscribeEvent.setToUserName(baseEvent.getToUserName());
            		return evenCallback.onReceive(subscribeEvent);
            		
				case MessageUtil.EVENT_TYPE_UNSUBSCRIBE:
					UnsubscribeEvent unsubscribeEvent=new UnsubscribeEvent();
					unsubscribeEvent.setFromUserName(baseEvent.getFromUserName());
					unsubscribeEvent.setToUserName(baseEvent.getToUserName());
					return evenCallback.onReceive(unsubscribeEvent);
					

				case MessageUtil.EVENT_TYPE_SCAN://二维码扫描
					QRCodeEvent qrCodeEvent=new QRCodeEvent();
					qrCodeEvent.setFromUserName(baseEvent.getFromUserName());
					qrCodeEvent.setToUserName(baseEvent.getToUserName());
					qrCodeEvent.setEventKey(requestMap.get("EventKey"));
					qrCodeEvent.setTicket(requestMap.get("Ticket"));
					
					return evenCallback.onReceive(qrCodeEvent);
				case MessageUtil.EVENT_TYPE_LOCATION://上报垃理位置
					LocationEvent locationEvent=new LocationEvent();
					locationEvent.setFromUserName(baseEvent.getFromUserName());
					locationEvent.setToUserName(baseEvent.getToUserName());
					locationEvent.setLatitude(requestMap.get("Latitude"));
					locationEvent.setLongitude(requestMap.get("Longitude"));
					locationEvent.setPrecision(requestMap.get("Precision"));
					
					return evenCallback.onReceive(locationEvent);
				}
            	
            }else if(messageCallback!=null){
            	BaseMessage message=new BaseMessage();
            	message.setFromUserName(fromUserName);
            	message.setMsgId(requestMap.get("MsgId"));
            	message.setCreateTime(Long.parseLong(requestMap.get("CreateTime")));
            	message.setMsgType(msgType);
            	message.setToUserName(toUserName);
               switch (msgType) {
				case MessageUtil.REQ_MESSAGE_TYPE_TEXT: // 文本消息
						TextMessage textMessage=new TextMessage();
						textMessage.setFromUserName(message.getFromUserName());
						textMessage.setToUserName(message.getToUserName());
						textMessage.setContent( requestMap.get("Content"));
						
					return messageCallback.onReceive(textMessage);
				case MessageUtil.REQ_MESSAGE_TYPE_IMAGE://图片消息
						ImageMessage imageMessage= new ImageMessage();
						imageMessage.setFromUserName(message.getFromUserName());
						imageMessage.setToUserName(message.getToUserName());
					    Image image=new Image();
						image.setMediaId(requestMap.get("MediaId"));
						imageMessage.setPicUrl(requestMap.get("PicUrl"));
						imageMessage.setImage(image);
						
					return messageCallback.onReceive(imageMessage);
				case MessageUtil.REQ_MESSAGE_TYPE_VOICE://语音消息
						VoiceMessage voiceMessage=new VoiceMessage();
						voiceMessage.setFromUserName(message.getFromUserName());
						voiceMessage.setToUserName(message.getToUserName());
						Voice voice=new Voice();
						voice.setMediaId(requestMap.get("MediaId"));
						voiceMessage.setVoice(voice);
						
					return messageCallback.onReceive(voiceMessage);
				case MessageUtil.REQ_MESSAGE_TYPE_VIDEO://视频消息
						VideoMessage videoMessage=new VideoMessage();
						videoMessage.setFromUserName(message.getFromUserName());
						videoMessage.setToUserName(message.getToUserName());
						Video video=new Video();
						video.setMediaId(requestMap.get("MediaId"));
						video.setThumbMediaId(requestMap.get("ThumbMediaId"));
						videoMessage.setVideo(video);
						
					return messageCallback.onReceive(videoMessage);
				case MessageUtil.REQ_MESSAGE_TYPE_SHORTVIDEO:// 视频消息
							VideoMessage videoMessage1=new VideoMessage();
							videoMessage1.setFromUserName(message.getFromUserName());
							videoMessage1.setToUserName(message.getToUserName());
							Video video1=new Video();
							video1.setMediaId(requestMap.get("MediaId"));
							video1.setThumbMediaId(requestMap.get("ThumbMediaId"));
							videoMessage1.setVideo(video1);
							
					return messageCallback.onReceive(videoMessage1);
			
				case MessageUtil.REQ_MESSAGE_TYPE_LINK://链接消息
						
					return "";
				}
          }
        	TextMessage textMessage = new TextMessage();
			textMessage.setToUserName(fromUserName);
			textMessage.setFromUserName(toUserName);
			textMessage.setCreateTime(new Date().getTime());
			textMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_TEXT);
			textMessage.setContent("未知消息");
			return MessageUtil.messageToXml(textMessage);
    }
}